/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.isfd.domain.IsfdModelManagementOriginalPart;
import com.comac.ins.isfd.domain.bo.IsfdModelManagementOriginalPartBo;
import com.comac.ins.isfd.domain.vo.IsfdModelManagementOriginalPartVo;
import com.comac.ins.isfd.mapper.IsfdModelManagementOriginalPartMapper;
import com.comac.ins.isfd.service.IIsfdModelManagementOriginalPartService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 模型管理原始装机清单Service业务层处理
 *
 * @author hxloongs
 * @date 2024-08-30
 */
@RequiredArgsConstructor
@Service
public class IsfdModelManagementOriginalPartServiceImpl implements IIsfdModelManagementOriginalPartService {

    @Autowired
    private IsfdModelManagementOriginalPartMapper baseMapper;

    /**
     * 查询模型管理原始装机清单
     *
     * @param id 主键
     * @return 模型管理原始装机清单
     */
    @Override
    public IsfdModelManagementOriginalPartVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询模型管理原始装机清单列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 模型管理原始装机清单分页列表
     */
    @Override
    public TableDataInfo<IsfdModelManagementOriginalPartVo> queryPageList(IsfdModelManagementOriginalPartBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IsfdModelManagementOriginalPart> lqw = buildQueryWrapper(bo);
        Page<IsfdModelManagementOriginalPartVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的模型管理原始装机清单列表
     *
     * @param bo 查询条件
     * @return 模型管理原始装机清单列表
     */
    @Override
    public List<IsfdModelManagementOriginalPartVo> queryList(IsfdModelManagementOriginalPartBo bo) {
        LambdaQueryWrapper<IsfdModelManagementOriginalPart> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IsfdModelManagementOriginalPart> buildQueryWrapper(IsfdModelManagementOriginalPartBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IsfdModelManagementOriginalPart> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getLevel() != null, IsfdModelManagementOriginalPart::getLevel, bo.getLevel());
        lqw.like(StringUtils.isNotBlank(bo.getMainSectionName()), IsfdModelManagementOriginalPart::getMainSectionName, bo.getMainSectionName());
        lqw.like(StringUtils.isNotBlank(bo.getSectionName()), IsfdModelManagementOriginalPart::getSectionName, bo.getSectionName());
        lqw.eq(StringUtils.isNotBlank(bo.getAta()), IsfdModelManagementOriginalPart::getAta, bo.getAta());
        lqw.eq(StringUtils.isNotBlank(bo.getPartNumber()), IsfdModelManagementOriginalPart::getPartNumber, bo.getPartNumber());
        lqw.like(StringUtils.isNotBlank(bo.getName()), IsfdModelManagementOriginalPart::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getVersionNumber()), IsfdModelManagementOriginalPart::getVersionNumber, bo.getVersionNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), IsfdModelManagementOriginalPart::getStatus, bo.getStatus());
        lqw.eq(bo.getAmount() != null, IsfdModelManagementOriginalPart::getAmount, bo.getAmount());
        lqw.eq(bo.getUnitWeight() != null, IsfdModelManagementOriginalPart::getUnitWeight, bo.getUnitWeight());
        lqw.eq(StringUtils.isNotBlank(bo.getImportantParts()), IsfdModelManagementOriginalPart::getImportantParts, bo.getImportantParts());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), IsfdModelManagementOriginalPart::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getEffectiveSorties()), IsfdModelManagementOriginalPart::getEffectiveSorties, bo.getEffectiveSorties());
        lqw.eq(StringUtils.isNotBlank(bo.getDciNumber()), IsfdModelManagementOriginalPart::getDciNumber, bo.getDciNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getOriginalDeformedPartsFlag()), IsfdModelManagementOriginalPart::getOriginalDeformedPartsFlag, bo.getOriginalDeformedPartsFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), IsfdModelManagementOriginalPart::getRemarks, bo.getRemarks());
        lqw.eq(StringUtils.isNotBlank(bo.getQuicklyChangeNumber()), IsfdModelManagementOriginalPart::getQuicklyChangeNumber, bo.getQuicklyChangeNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getEcpNumber()), IsfdModelManagementOriginalPart::getEcpNumber, bo.getEcpNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getSupplierQuipmentNumber()), IsfdModelManagementOriginalPart::getSupplierQuipmentNumber, bo.getSupplierQuipmentNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getSupplierVersionNumber()), IsfdModelManagementOriginalPart::getSupplierVersionNumber, bo.getSupplierVersionNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getSupplierNumber()), IsfdModelManagementOriginalPart::getSupplierNumber, bo.getSupplierNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getEtsiNumber()), IsfdModelManagementOriginalPart::getEtsiNumber, bo.getEtsiNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getEtsiVersion()), IsfdModelManagementOriginalPart::getEtsiVersion, bo.getEtsiVersion());
        lqw.eq(StringUtils.isNotBlank(bo.getEtsiRemarks()), IsfdModelManagementOriginalPart::getEtsiRemarks, bo.getEtsiRemarks());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterial()), IsfdModelManagementOriginalPart::getMaterial, bo.getMaterial());
        lqw.in(!CollectionUtils.isEmpty(bo.getSectionNameList()), IsfdModelManagementOriginalPart::getSectionName, bo.getSectionNameList());
        return lqw;
    }

    /**
     * 新增模型管理原始装机清单
     *
     * @param bo 模型管理原始装机清单
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(IsfdModelManagementOriginalPartBo bo) {
        IsfdModelManagementOriginalPart add = MapstructUtils.convert(bo, IsfdModelManagementOriginalPart.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改模型管理原始装机清单
     *
     * @param bo 模型管理原始装机清单
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(IsfdModelManagementOriginalPartBo bo) {
        IsfdModelManagementOriginalPart update = MapstructUtils.convert(bo, IsfdModelManagementOriginalPart.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IsfdModelManagementOriginalPart entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除模型管理原始装机清单信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
